// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   apply_draft_ok.java

package draft.manufacture;

import include.nseer_cookie.counter;
import include.nseer_cookie.exchange;
import include.nseer_db.nseer_db_backup1;
import java.io.IOException;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.jsp.JspFactory;
import validata.ValidataNumber;

public class apply_draft_ok extends HttpServlet
{

	public apply_draft_ok()
	{
	}

	public synchronized void service(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		HttpSession dbSession = request.getSession();
		JspFactory _jspxFactory = JspFactory.getDefaultFactory();
		javax.servlet.jsp.PageContext pageContext = _jspxFactory.getPageContext(this, request, response, "", true, 8192, true);
		javax.servlet.ServletContext dbApplication = dbSession.getServletContext();
		HttpSession session = request.getSession();
		nseer_db_backup1 manufacture_db = new nseer_db_backup1(dbApplication);
		counter count = new counter(dbApplication);
		ValidataNumber validata = new ValidataNumber();
		try
		{
			if (manufacture_db.conn((String)dbSession.getAttribute("unit_db_name")))
			{
				String apply_ID = request.getParameter("apply_ID");
				String product_IDn[] = request.getParameterValues("product_ID");
				String amount1[] = request.getParameterValues("amount");
				String product_amount = request.getParameter("product_amount");
				int num = Integer.parseInt(product_amount);
				int m = 0;
				int n = 0;
				for (int i = 1; i <= num; i++)
				{
					String tem_amount = (new StringBuilder("amount")).append(i).toString();
					String amount = request.getParameter(tem_amount);
					if (!validata.validata(amount))
						n++;
				}

				for (int j = 1; j < product_IDn.length; j++)
				{
					String sql3 = (new StringBuilder("select * from manufacture_apply where apply_ID='")).append(apply_ID).append("' and product_ID='").append(product_IDn[j]).append("'").toString();
					ResultSet rs3 = manufacture_db.executeQuery(sql3);
					if (rs3.next())
						m++;
					if (!validata.validata(amount1[j]))
						n++;
				}

				if (num == 0 && product_IDn.length == 1)
					response.sendRedirect((new StringBuilder("draft/manufacture/apply_ok_a.jsp?apply_ID=")).append(apply_ID).toString());
				else
				if (m != 0)
					response.sendRedirect((new StringBuilder("draft/manufacture/apply_ok_b.jsp?apply_ID=")).append(apply_ID).toString());
				else
				if (n == 0)
				{
					String reason = request.getParameter("reason");
					String register = request.getParameter("register");
					String register_time = request.getParameter("register_time");
					String bodyb = new String(request.getParameter("remark").getBytes("UTF-8"), "UTF-8");
					String remark = exchange.toHtml(bodyb);
					try
					{
						String sql1 = (new StringBuilder("delete from manufacture_apply where apply_ID='")).append(apply_ID).append("' and product_ID=''").toString();
						manufacture_db.executeUpdate(sql1);
						for (int i = 1; i <= num; i++)
						{
							String tem_real_cost_price = (new StringBuilder("real_cost_price")).append(i).toString();
							String tem_product_name = (new StringBuilder("product_name")).append(i).toString();
							String tem_product_ID = (new StringBuilder("product_ID")).append(i).toString();
							String tem_product_describe = (new StringBuilder("product_describe")).append(i).toString();
							String tem_amount = (new StringBuilder("amount")).append(i).toString();
							String real_cost_price = request.getParameter(tem_real_cost_price);
							String product_name = request.getParameter(tem_product_name);
							String product_ID = request.getParameter(tem_product_ID);
							String product_describe = request.getParameter(tem_product_describe);
							String amount = request.getParameter(tem_amount);
							sql1 = (new StringBuilder("update manufacture_apply set product_ID='")).append(product_ID).append("',product_name='").append(product_name).append("',product_describe='").append(product_describe).append("',amount='").append(amount).append("',remark='").append(remark).append("',pay_id_group='").append(reason).append("',register='").append(register).append("',register_time='").append(register_time).append("' where apply_ID='").append(apply_ID).append("' and product_ID='").append(product_ID).append("'").toString();
							manufacture_db.executeUpdate(sql1);
						}

						String amount_unitn[] = request.getParameterValues("amount_unit");
						String real_cost_pricen[] = request.getParameterValues("real_cost_price");
						String product_namen[] = request.getParameterValues("product_name");
						String product_describen[] = request.getParameterValues("product_describe");
						String type[] = request.getParameterValues("type");
						for (int j = 1; j < product_IDn.length; j++)
						{
							String sql4 = (new StringBuilder("insert into manufacture_apply(apply_ID,product_ID,product_name,product_describe,amount,remark,type,pay_id_group,register,register_time,check_tag) values('")).append(apply_ID).append("','").append(product_IDn[j]).append("','").append(product_namen[j]).append("','").append(product_describen[j]).append("','").append(amount1[j]).append("','").append(remark).append("','").append(type[j]).append("','").append(reason).append("','").append(register).append("','").append(register_time).append("','5')").toString();
							manufacture_db.executeUpdate(sql4);
						}

					}
					catch (Exception ex)
					{
						ex.printStackTrace();
					}
					response.sendRedirect("draft/manufacture/apply_ok.jsp?finished_tag=0");
				} else
				{
					response.sendRedirect((new StringBuilder("draft/manufacture/apply_ok_c.jsp?apply_ID=")).append(apply_ID).toString());
				}
				manufacture_db.commit();
				manufacture_db.close();
			} else
			{
				response.sendRedirect("error_conn.htm");
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}
